<?php

namespace App\Listeners;

use Swoole\WebSocket\Server;
use Swoole\WebSocket\Frame;
use Laravel\Octane\Facades\Octane;
class WebSocketEventListener
{
    public function handleMessage(Server $server, Frame $frame): void
    {
        if (Octane::table('websocket_connections')->get($frame->fd)) {
            $data = json_decode($frame->data, true);
            $server->push($frame->fd, json_encode(['response' => $data['message']]));
        }
    }

    public function handleClose(Server $server, int $fd): void
    {
        Octane::table('websocket_connections')->delete($fd);
    }
}
